var express = require('express');
var router = express.Router();
const QuestModel = require("../db/questModel");
const InfoModel = require("../db/infoModel");

router.post('/', async (req, res) => {
    const {title,description,questions} = req.body
    const date = Date.now()
    const info = await InfoModel.find({"key":"questId"},{"_id":0})
    const questid = info[0]._doc["value"]

    const questArray = eval(questions)

    const aquest = new QuestModel({
        "username":req.user.username,
        "id":questid,
        "openCode":null,
        "title":title,
        "description":description,
        "status":"1",
        "lastEditedTime":date,
        "releaseTime":null,
        "endTime":null,
        "questions":questArray
    })
    await aquest.save(function (error,ret){
        if(error){
            res.send({
                "code":0,
                "id":questid,
                "msg":error
            })}
        else{
            InfoModel.updateOne({"key":"questId"},{$set:{"value":questid+1}},function (errorr,data){
            })//缺错误处理
            res.send({
                "code":1,
                "id":questid,
                "msg":"创建成功"
            })
        }
    })
})

module.exports = router;
